package com.zmn.oms.business.impl.factorypart;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.factorypart.FactoryPartBService;
import com.zmn.oms.common.constant.OrderFactoryPartConsts;
import com.zmn.oms.model.entity.fcorderpart.OrderFactoryPart;
import com.zmn.oms.model.entity.fcorderpart.OrderFactoryPartPost;
import com.zmn.oms.model.vo.factorypart.NewPartVO;
import com.zmn.oms.model.vo.factorypart.UsedPartVO;
import com.zmn.oms.services.interfaces.fcorderpart.OrderFactoryPartPostService;
import com.zmn.oms.services.interfaces.fcorderpart.OrderFactoryPartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：厂商配件业务
 *
 * @author wangxiaokun
 * @since 2020/9/11 18:40
 */
@Service
public class FactoryPartBServiceImpl implements FactoryPartBService {

    @Autowired
    private OrderFactoryPartPostService orderFactoryPartPostService;
    @Autowired
    private OrderFactoryPartService orderFactoryPartService;

    @Override
    public List<NewPartVO> listNewPartByOrderIdAndFactoryId(Long orderId) {

        List<OrderFactoryPart> orderFactoryParts = orderFactoryPartService.listOrderFactoryPartByOrderId(orderId);
        // 所有物流信息
        List<OrderFactoryPartPost> allPosts = orderFactoryPartPostService.listOrderFactoryPartPostByOrderId(orderId);

        // 过滤旧件返厂物流
        List<OrderFactoryPartPost> newPartPosts = allPosts.stream()
                .filter(post -> Objects.equals(post.getFcOrderPartPostType(), 1)).collect(Collectors.toList());

        if (CollectionUtil.isNullOrEmpty(newPartPosts)){
            return Lists.newArrayList();
        }

        List<NewPartVO> newPartVOS = BeanMapper.mapList(newPartPosts, NewPartVO.class);

        // 将新件物流转为map
        Map<Integer, List<OrderFactoryPart>> map = orderFactoryParts.stream()
                .collect(Collectors.groupingBy(OrderFactoryPart::getFcOrderPartId));

        // 物流信息
        newPartVOS.forEach(vo -> {
            List<OrderFactoryPart> parts = map.get(vo.getFcOrderPartId());
            if (CollectionUtil.isNotNullOrEmpty(parts)){
                OrderFactoryPart part = parts.get(0);
                vo.setFcOrderPartId(part.getFcOrderPartId());
                vo.setPartId(part.getPartId());
                vo.setPartFullName(part.getPartFullName());
                vo.setNumber(part.getNumber());
                vo.setApplyTypeName(OrderFactoryPartConsts.getApplyTypeName(part.getApplyType()));
                vo.setUnit(part.getUnit());
                vo.setStatusName(GlobalConsts.getPostStatusName(vo.getStatus()));
            }
        });
        return newPartVOS;
    }

    @Override
    public List<UsedPartVO> listUsedPartByOrderIdAndFactoryId(Long orderId) {

        List<OrderFactoryPart> orderFactoryParts = orderFactoryPartService.listOrderFactoryPartByOrderId(orderId);
        // 所有物流信息
        List<OrderFactoryPartPost> allPosts = orderFactoryPartPostService.listOrderFactoryPartPostByOrderId(orderId);

        // 过滤旧件返厂物流
        List<OrderFactoryPartPost> usedPartPosts = allPosts.stream()
                .filter(post -> Objects.equals(post.getFcOrderPartPostType(), 2)).collect(Collectors.toList());

        if (CollectionUtil.isNullOrEmpty(usedPartPosts)){
            return Lists.newArrayList();
        }
        List<UsedPartVO> usedPartVOS = BeanMapper.mapList(usedPartPosts, UsedPartVO.class);
        // 将新件物流转为map
        Map<Integer, List<OrderFactoryPart>> map = orderFactoryParts.stream()
                .collect(Collectors.groupingBy(OrderFactoryPart::getFcOrderPartId));

        // 物流信息
        usedPartVOS.forEach(vo -> {
            List<OrderFactoryPart> parts = map.get(vo.getFcOrderPartId());
            if (CollectionUtil.isNotNullOrEmpty(parts)) {
                OrderFactoryPart part = parts.get(0);
                vo.setPartFullName(part.getPartFullName());
                vo.setNumber(part.getNumber());
                vo.setApplyTypeName("工程师申请");
                vo.setUnit(part.getUnit());
            }
        });
        return usedPartVOS;
    }
}
